package com.bsh.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsh.api.Exception.ApiException;
import com.bsh.service.ApiPartnerService;
import com.bsh.utils.ApiMessage;
import com.bsh.utils.Constants;

/**
 * 合伙人记录相关API
 * 
 * @author dl
 * 
 */
@Controller
@RequestMapping(value = "/api/v1/partner", method = { RequestMethod.POST })
public class ApiPartnerController {
	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	HttpSession session;
	@Autowired
	ApiPartnerService apiPartnerService;

	/**
	 * 添加合伙人
	 * 
	 * @param dto
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public ApiMessage savePartner(
			@RequestParam(value = "paramStr", required = true) String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiPartnerService.savePartner(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage(Constants.SYS_ERROR,Constants.MSG_FAILED);
			e.printStackTrace();
		} finally {
			return msg;
		}
	}

	/**
	 * 查询合伙人申请记录最新一条
	 * 
	 * @param dto
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/check", method = RequestMethod.POST)
	@ResponseBody
	public ApiMessage checkPartner(
			@RequestParam(value = "paramStr", required = true) String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiPartnerService.checkPartner(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage(Constants.SYS_ERROR,Constants.MSG_FAILED);
			e.printStackTrace();
		} finally {
			return msg;
		}
	
	}
	/**
	 * 升级合伙人
	 * 
	 * @param dto
	 * @return
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/upgrade", method = RequestMethod.POST)
	@ResponseBody
	public ApiMessage upgradePartner(
			@RequestParam(value = "paramStr", required = true) String paramStr) {
		ApiMessage msg = new ApiMessage();
		try {
			JSONObject dto = (JSONObject) JSON.parse(paramStr);
			msg = apiPartnerService.upgradePartner(dto);
		} catch (ApiException e) {
			msg.setCode(e.getCode());
			msg.setMessage(e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage(Constants.SYS_ERROR,Constants.MSG_FAILED);
			e.printStackTrace();
		} finally {
			return msg;
		}
	}

}
